package ro.msg.quiz.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import ro.msg.quiz.Connection.ConnectJdbc;
import ro.msg.quiz.Model.IQuizModel;
import ro.msg.quiz.Model.QuizJdbcModel;
import ro.msg.quiz.repository.Question;
import ro.msg.quiz.repository.Quiz;
import ro.msg.quiz.repository.RepositoryQuestion;
import ro.msg.quiz.repository.RepositoryQuiz;

/**
 * 
 * @author Adorjan
 * 
 */
public class Controller {
	private IQuizModel model;
	private ConnectJdbc conn;
	private QuizJdbcModel dbc;

	/**
	 * constructor
	 * 
	 * @param model
	 */
	public Controller(IQuizModel model) {
		this.model = model;
		this.conn = new ConnectJdbc();
		this.dbc = new QuizJdbcModel();
	}

	/**
	 * @return the model
	 */
	public IQuizModel getModel() {
		return model;
	}

	public void setModel(IQuizModel model) {
		this.model = model;
	}

	/**
	 * 
	 * @return the list of questions
	 */
	public ArrayList<Question> getQuestionList() {
		return model.getQuestionRepo().getQuestionsList();
	}

	/**
	 * adds a question to the repository
	 * 
	 * @param question
	 */
	public void addQuestion(Question question) {
		model.addQuestion(question);
	}

	/**
	 * removes the question from the repository if removed, returns true else
	 * false - if it didn't existed
	 * 
	 * @param question
	 * @return
	 */
	public boolean removeQuestion(int question) {
		return model.removeQuestion(question);
	}

	/**
	 * adds a quiz to the repository
	 * 
	 * @param quiz
	 */
	public void addQuiz(Quiz quiz) {
		model.addQuiz(quiz);
	}

	/**
	 * removes a quiz from the repository and the map too
	 * 
	 * @param quiz
	 */
	public void removeQuiz(int quiz) {
		model.removeQuiz(quiz);
	}

	/**
	 * 
	 * @return the list of quizes
	 */
	public ArrayList<Quiz> getQuizList() {
		return model.getQuizRepo().getQuizes();
	}

	/**
	 * returns a quiz from the repo with an id of quizId
	 * 
	 * @param quiz
	 * @return
	 */
	public Quiz getFromQuizList(int quizId) {
		for (Quiz q : getQuizList()) {
			if (q.getId() == quizId)
				return q;
		}
		return null;
	}

	/**
	 * returns a question from the repo
	 * 
	 * @param questionId
	 * @return
	 */
	public Question getFromQuestionList(int questionId) {
		return model.getQuestionRepo().getQuestion(questionId);
	}

	/**
	 * 
	 * @return the highest ID from the question repository
	 */
	public int getLastIdQuestionRepo() {
		int last = -1;
		for (Question question : model.getQuestionRepo().getQuestionsList()) {
			if (last < question.getId())
				last = question.getId();
		}
		return last;
	}

	/**
	 * 
	 * @return the highest ID from the quiz repository
	 */
	public int getLastIdQuizRepo() {
		int last = -1;
		for (Quiz quiz : model.getQuizRepo().getQuizes()) {
			if (last < quiz.getId())
				last = quiz.getId();
		}
		return last;
	}

	/**
	 * bring in the writeToFile methods
	 */
	public void ExportTxt(String path) {
		model.ExportTxt(path);
	}

	/**
	 * This method returns the map of the quiz ids and their questions
	 * 
	 * @return
	 */
	public Map<Integer, ArrayList<Integer>> getQuizQuestions() {
		return model.getQuizQuestions();
	}

	/**
	 * This method returns the number of questions that belong to a specific
	 * quiz
	 * 
	 * @param quiz
	 * @return
	 */
	public Integer getNoOfQuestions(Integer quiz) {
		if (getQuizQuestions().containsKey(quiz))
			return getQuizQuestions().get(quiz).size();
		else
			return 0;
	}

	/**
	 * refreshes the map of relations for the specified quiz with a new list of
	 * questions
	 * 
	 * @param quiz
	 * @param list
	 */
	public void addQuestionsQuiz(Integer quiz, ArrayList<Integer> list) {
		if (getQuizQuestions().containsKey(quiz)) {
			for (int i = 0; i < getQuizQuestions().get(quiz).size(); i++)
				getQuizQuestions().get(quiz).remove(i);
		}
		for (Integer id : list) {
			model.addQuestionToQuiz(quiz, id);
		}
	}

	/**
	 * calls the read of quizes
	 */
	public void readFromFileQuiz(String path) {
		model.getQuizRepo().readFromFileQuiz(path);
	}

	/**
	 * calls the write of quizes
	 */
	public void writeToFileQuiz(String path) {
		model.getQuizRepo().writeToFileQuiz(path);
	}

	/**
	 * calls the write of questions
	 * 
	 * @param path
	 */
	public void readFromFileQuestion(String path) {
		model.getQuestionRepo().readFromFileQuestions(path);
	}

	/**
	 * calls the write of questions
	 * 
	 * @param path
	 */
	public void writeToFileQuestion(String path) {
		model.getQuestionRepo().writeToFileQuestions(path);
	}

	/**
	 * calls the read of quizQuestion
	 * 
	 * @param path
	 */
	public void readFromFileQuizQuestion(String path) {
		model.readFromFileQuizQuestion(path);
	}

	/**
	 * calls the write of quizQuestion
	 * 
	 * @param path
	 */
	public void writeToFileQuizQuestion(String path) {
		model.writeToFileQuizQuestion(path);
	}

	/**
	 * returns the list of question ids that belong to "quiz"
	 * 
	 * @param quiz
	 * @return
	 */
	public ArrayList<Integer> getQuestions(int quiz) {
		ArrayList<Integer> questionIds = new ArrayList<Integer>();
		for (Integer question : model.getQuizQuestions().get(quiz)) {
			questionIds.add(question);
		}
		return questionIds;
	}

	public ConnectJdbc getConnection() {
		return conn;
	}

	public void setConn(ConnectJdbc conn) {
		this.conn = conn;
	}

	public QuizJdbcModel getDatabase() {
		return dbc;
	}

	public void setDatabase(QuizJdbcModel dbc) {
		this.dbc = dbc;
	}

	/**
	 * This method clears the repositories
	 */
	public void clearRepositories() {
		Map<Integer, ArrayList<Integer>> quizQuestions = new HashMap<Integer, ArrayList<Integer>>();
		model.setQuestionRepo(new RepositoryQuestion());
		model.setQuizRepo(new RepositoryQuiz());
		model.setQuizQuestions(quizQuestions);
	}
}
